<!DOCTYPE html>
<html lang="zh-cn">
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <title>如何在Linux命令行下发送和接收UDP数据包 - whowin - 发表我个人原创作品的技术博客</title>
  <meta name="renderer" content="webkit" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>

<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />

<meta name="theme-color" content="#f8f5ec" />
<meta name="msapplication-navbutton-color" content="#f8f5ec">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="#f8f5ec">


<meta name="author" content="whowin" /><meta name="description" content="众所周知，在传输层有两个常用的协议 TCP 和 UDP，本文介绍在 Linux 命令行下，如何使用 nc 命令发送或接收 UDP 数据包，这些命令的用法对调试 UDP 通信程序将有所帮助，本文适合初学者阅读。
" /><meta name="keywords" content="linux, socket, hugo, dos" />






<meta name="generator" content="Hugo 0.97.3 with theme even" />


<link rel="canonical" href="https://whowin.gitee.io/post/blog/network/0005-send-udp-via-linux-cli/" />
<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png">
<link rel="manifest" href="/manifest.json">
<link rel="mask-icon" href="/safari-pinned-tab.svg" color="#5bbad5">



<link href="/sass/main.min.e3fea119b1980e848b03dffbeddb11dd0fba483eed0e5f11870fb8e31f145bbd.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.1.20/dist/jquery.fancybox.min.css" integrity="sha256-7TyXnr2YU040zfSP+rEcz29ggW4j56/ujTPwjMzyqFY=" crossorigin="anonymous">


<meta property="og:title" content="如何在Linux命令行下发送和接收UDP数据包" />
<meta property="og:description" content="众所周知，在传输层有两个常用的协议 TCP 和 UDP，本文介绍在 Linux 命令行下，如何使用 nc 命令发送或接收 UDP 数据包，这些命令的用法对调试 UDP 通信程序将有所帮助，本文适合初学者阅读。" />
<meta property="og:type" content="article" />
<meta property="og:url" content="https://whowin.gitee.io/post/blog/network/0005-send-udp-via-linux-cli/" /><meta property="article:section" content="post" />
<meta property="article:published_time" content="2022-12-10T16:43:29+08:00" />
<meta property="article:modified_time" content="2022-12-10T16:43:29+08:00" />

<meta itemprop="name" content="如何在Linux命令行下发送和接收UDP数据包">
<meta itemprop="description" content="众所周知，在传输层有两个常用的协议 TCP 和 UDP，本文介绍在 Linux 命令行下，如何使用 nc 命令发送或接收 UDP 数据包，这些命令的用法对调试 UDP 通信程序将有所帮助，本文适合初学者阅读。"><meta itemprop="datePublished" content="2022-12-10T16:43:29+08:00" />
<meta itemprop="dateModified" content="2022-12-10T16:43:29+08:00" />
<meta itemprop="wordCount" content="952">
<meta itemprop="keywords" content="Linux,ubuntu,netcat,nc,udp packet," /><meta name="twitter:card" content="summary"/>
<meta name="twitter:title" content="如何在Linux命令行下发送和接收UDP数据包"/>
<meta name="twitter:description" content="众所周知，在传输层有两个常用的协议 TCP 和 UDP，本文介绍在 Linux 命令行下，如何使用 nc 命令发送或接收 UDP 数据包，这些命令的用法对调试 UDP 通信程序将有所帮助，本文适合初学者阅读。"/>

<!--[if lte IE 9]>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/classlist/1.1.20170427/classList.min.js"></script>
<![endif]-->

<!--[if lt IE 9]>
  <script src="https://cdn.jsdelivr.net/npm/html5shiv@3.7.3/dist/html5shiv.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/respond.js@1.4.2/dest/respond.min.js"></script>
<![endif]-->

  <script async src="/js/busuanzi.pure.mini.js"></script><script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-9724909319263152"
     crossorigin="anonymous"></script>


</head>
<body>
  <div id="mobile-navbar" class="mobile-navbar">
  <div class="mobile-header-logo">
    <a href="/" class="logo">WhoWin</a>
  </div>
  <div class="mobile-navbar-icon">
    <span></span>
    <span></span>
    <span></span>
  </div>
</div>
<nav id="mobile-menu" class="mobile-menu slideout-menu">
  <ul class="mobile-menu-list">
    <a href="/">
        <li class="mobile-menu-item">首页</li>
      </a><a href="/post/">
        <li class="mobile-menu-item">文章归档</li>
      </a><a href="/article-categories/categories/">
        <li class="mobile-menu-item">文章分类</li>
      </a><a href="/tags/">
        <li class="mobile-menu-item">文章标签</li>
      </a><a href="/about/about/">
        <li class="mobile-menu-item">关于</li>
      </a>
  </ul>

  


</nav>

  <div class="container" id="mobile-panel">
    <header id="header" class="header">
        <div class="logo-wrapper">
  <a href="/" class="logo">WhoWin</a>
  
  <div style="position:absolute; left: 80px; top: 75px; color: crimson">
      ———开源和分享是技术发展的源泉和动力；本博客所有文章均为原创
  </div>
</div>





<nav class="site-navbar">
  <ul id="menu" class="menu">
    <li class="menu-item">
        <a class="menu-item-link" href="/">首页</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/post/">文章归档</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/article-categories/categories/">文章分类</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/tags/">文章标签</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/about/about/">关于</a>
      </li>
  </ul>
</nav>

    </header>

    <main id="main" class="main">
      <div class="content-wrapper">
        <div id="content" class="content">
          <article class="post">
    
    <header class="post-header">
      <h1 class="post-title">如何在Linux命令行下发送和接收UDP数据包</h1>

      <div class="post-meta">
        <span class="post-time"> 2022-12-10 </span>
        <div class="post-category">
            <a href="/categories/linux/"> Linux </a>
            <a href="/categories/network/"> Network </a>
            </div>
        
      </div>
    </header>

    <div class="post-toc" id="post-toc">
  <h2 class="post-toc-title">文章目录</h2>
  <div class="post-toc-content always-active">
    <nav id="TableOfContents">
  <ul>
    <li>
      <ul>
        <li><a href="#1-问题的提出">1. 问题的提出</a></li>
        <li><a href="#2-文章要点">2. 文章要点</a></li>
        <li><a href="#3-netcat-命令">3. netcat 命令</a></li>
        <li><a href="#4-发送-udp-数据包">4. 发送 UDP 数据包</a></li>
        <li><a href="#5-其它可以发送-udp-数据包的命令">5. 其它可以发送 UDP 数据包的命令</a></li>
        <li><a href="#欢迎订阅-网络编程专栏httpsblogcsdnnetwhowincategory_12180345html"><strong>欢迎订阅 <a href="https://blog.csdn.net/whowin/category_12180345.html">『网络编程专栏』</a></strong></a></li>
      </ul>
    </li>
  </ul>
</nav>
  </div>
</div>
    <div class="post-content">
      <p>众所周知，在传输层有两个常用的协议 TCP 和 UDP，本文介绍在 Linux 命令行下，如何使用 nc 命令发送或接收 UDP 数据包，这些命令的用法对调试 UDP 通信程序将有所帮助，本文适合初学者阅读。</p>
<h2 id="1-问题的提出">1. 问题的提出</h2>
<blockquote>
<p>编写了一个使用 raw socket 在数据链路层接收原始 UDP 数据包的程序，调试的时候，需要使用局域网中的另一台机器发送 UDP 数据包，突然发现居然一下子想不出如何在命令行上发送数据包；首先想到的是用 curl，但又一想不对，curl 只能使用应用层协议透过 TCP 协议发送数据包，所以可以发 HTTP 请求包，FTP 请求包等，是不能发送 UDP 数据包的。</p>
</blockquote>
<blockquote>
<p>终于想起了不怎么使用的 nc 命令，可以很方便地发送 UDP 数据包</p>
</blockquote>
<h2 id="2-文章要点">2. 文章要点</h2>
<ol>
<li>理解 Linux 下的 nc 命令；</li>
<li>使用 nc 命令在网络上发送或接收 UDP 报文；</li>
<li>抓取 nc 命令发送的 UDP 报文；</li>
<li>介绍其它可以发送 UDP 报文的命令。</li>
</ol>
<h2 id="3-netcat-命令">3. netcat 命令</h2>
<ul>
<li>nc 是 netcat 的简写，大多数的 Linux 发行版中默认是安装 netcat 的，打开一个终端窗口(ctrl+alt_t)，用下面命令检查一下是否已经安装了 netcat：
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">nc
</span></span></code></pre></td></tr></table>
</div>
</div></li>
<li>在 ubuntu 20.04 下的输出：
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">usage: nc <span class="o">[</span>-46CDdFhklNnrStUuvZz<span class="o">]</span> <span class="o">[</span>-I length<span class="o">]</span> <span class="o">[</span>-i interval<span class="o">]</span> <span class="o">[</span>-M ttl<span class="o">]</span>
</span></span><span class="line"><span class="cl">          <span class="o">[</span>-m minttl<span class="o">]</span> <span class="o">[</span>-O length<span class="o">]</span> <span class="o">[</span>-P proxy_username<span class="o">]</span> <span class="o">[</span>-p source_port<span class="o">]</span>
</span></span><span class="line"><span class="cl">          <span class="o">[</span>-q seconds<span class="o">]</span> <span class="o">[</span>-s source<span class="o">]</span> <span class="o">[</span>-T keyword<span class="o">]</span> <span class="o">[</span>-V rtable<span class="o">]</span> <span class="o">[</span>-W recvlimit<span class="o">]</span> <span class="o">[</span>-w timeout<span class="o">]</span>
</span></span><span class="line"><span class="cl">          <span class="o">[</span>-X proxy_protocol<span class="o">]</span> <span class="o">[</span>-x proxy_address<span class="o">[</span>:port<span class="o">]]</span> 	  <span class="o">[</span>destination<span class="o">]</span> <span class="o">[</span>port<span class="o">]</span>
</span></span></code></pre></td></tr></table>
</div>
</div></li>
<li>这个输出表示当前的 Linux 下已经有 nc 命令了。</li>
</ul>
<h2 id="4-发送-udp-数据包">4. 发送 UDP 数据包</h2>
<ul>
<li>
<p>举个例子，假定我们要从 A 机发送一个 UDP 报文到 B 机，按照 server-client 的概念，我们把 B 机作为 server 端，A 机作为 client 端；</p>
<p><img src="https://whowin.gitee.io/images/180005/server_client_connection.png" alt="Server-client方式连接的两台机器"></p>
</li>
<li>
<p>A 机的 IP: 192.168.2.112；B 机的 IP：192.168.2.114</p>
</li>
<li>
<p><strong>启动 server</strong></p>
<ul>
<li>
<p>在 B 机上用如下命令启动 Server</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">nc –u –l <span class="m">8888</span>
</span></span></code></pre></td></tr></table>
</div>
</div></li>
<li>
<p>下面是屏幕截图</p>
<p><img src="https://whowin.gitee.io/images/180005/start_server_with_nc.png" alt="在B机用nc启动服务器监听"></p>
</li>
<li>
<p>启动后，并没有更多的提示，当收到信息时，会显示收到的信息</p>
</li>
</ul>
</li>
<li>
<p><strong>检查 server 是否启动</strong></p>
<ul>
<li>
<p>在 B 机上启动一个新的终端窗口；</p>
</li>
<li>
<p>在 B 机上使用如下命令检查是否已经启动 server；</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">netstat -a<span class="p">|</span>grep <span class="m">8888</span>
</span></span></code></pre></td></tr></table>
</div>
</div></li>
<li>
<p>屏幕截图</p>
<p><img src="https://whowin.gitee.io/images/180005/screenshot_for_started_server.png" alt="检查server是否启动"></p>
</li>
</ul>
</li>
<li>
<p><strong>发送 UDP 报文</strong></p>
<ul>
<li>
<p>在 A 机上启动 client</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">nc -n 192.168.2.114 <span class="m">8888</span>
</span></span></code></pre></td></tr></table>
</div>
</div></li>
<li>
<p>client 启动后，和 server 端一样，并没有更多的提示，可以直接输入你要发出的信息</p>
</li>
<li>
<p>在 A 机输入信息：I am from ubuntu system A.</p>
</li>
<li>
<p>下面是 A 机上的屏幕截图</p>
<p><img src="https://whowin.gitee.io/images/180005/send_udp_packet_client.png" alt="在client端发送信息"></p>
</li>
<li>
<p>下面是 B 机上的屏幕截图</p>
<p><img src="https://whowin.gitee.io/images/180005/receive_udp_packet_server.png" alt="在server端接收信息"></p>
</li>
<li>
<p>也可以在 B 机输入信息，信息会发送到 A 机</p>
</li>
<li>
<p>在 B 机输入信息：I am from ubuntu system B.</p>
</li>
<li>
<p>下面是在 B 机的截图</p>
<p><img src="https://whowin.gitee.io/images/180005/send_udp_packet_server.png" alt="在server端发送信息"></p>
</li>
<li>
<p>下面是在 A 机的截图</p>
<p><img src="https://whowin.gitee.io/images/180005/receive_udp_packet_client.png" alt="在client端接收信息"></p>
</li>
</ul>
</li>
</ul>
<h2 id="5-其它可以发送-udp-数据包的命令">5. 其它可以发送 UDP 数据包的命令</h2>
<ul>
<li>
<p>还有一个可以简单地发送 UDP 报文的方法，我们在 A 机上退出 nc，然后使用下面命令发出信息</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl"><span class="nb">echo</span> -n <span class="s2">&#34;I am sending an UDP packet using echo command&#34;</span> &gt; /dev/udp/192.168.2.114/8888
</span></span></code></pre></td></tr></table>
</div>
</div></li>
<li>
<p>下面是在 A 机上的截屏</p>
<p><img src="https://whowin.gitee.io/images/180005/send_udp_packet_client_echo.png" alt="使用echo发送UDP报文"></p>
</li>
<li>
<p>下面是在 B 机上的截屏</p>
<p><img src="https://whowin.gitee.io/images/180005/receive_udp_packet_server_echo.png" alt="接收使用echo发送的UDP报文"></p>
</li>
<li>
<p>很显然使用 cat 命令也可以发送 UDP 报文：</p>
<p><img src="https://whowin.gitee.io/images/180005/send_udp_packet_client_cat.png" alt="使用cat命令发送UDP报文"></p>
<p><img src="https://whowin.gitee.io/images/180005/receive_udp_packet_server_cat.png" alt="接收使用cat命令发送的报文"></p>
</li>
</ul>
<h2 id="欢迎订阅-网络编程专栏httpsblogcsdnnetwhowincategory_12180345html"><strong>欢迎订阅 <a href="https://blog.csdn.net/whowin/category_12180345.html">『网络编程专栏』</a></strong></h2>
<hr>
<p><strong>欢迎访问我的博客：https://whowin.cn</strong></p>
<p><strong>email: <a href="mailto:hengch@163.com">hengch@163.com</a></strong></p>
<p><img src="https://whowin.gitee.io/images/qrcode/sponsor-qrcode.png" alt="donation"></p>
<!-- CSDN
[img01]:https://img-blog.csdnimg.cn/img_convert/369c015ee73c25e930c97f28feff97fe.png
[img02]:https://img-blog.csdnimg.cn/img_convert/a94450d7a118e06942e11cd3bee15b88.png
[img03]:https://img-blog.csdnimg.cn/img_convert/19d5de03be57aee2b04a33562ffda33b.png
[img04]:https://img-blog.csdnimg.cn/img_convert/bb6b617d6d89c52f07d9c1ae07bc9cee.png
[img05]:https://img-blog.csdnimg.cn/img_convert/204ea2b853a84b082a52841efe84e594.png
[img06]:https://img-blog.csdnimg.cn/img_convert/45abf0daf3d6af7486a55e81291f1dda.png
[img07]:https://img-blog.csdnimg.cn/img_convert/3ba1c8ddf249d7b76f15f25cefb78c13.png
[img08]:https://img-blog.csdnimg.cn/img_convert/f23656ae2dd4b30a1fa23d8e168eea6f.png
[img09]:https://img-blog.csdnimg.cn/img_convert/488b296f5e4b3a3f4ba699a2a374eb25.png
[img10]:https://img-blog.csdnimg.cn/img_convert/c6d7a298a76f29e727e89cefe7efdb12.png
[img11]:https://img-blog.csdnimg.cn/img_convert/70af03a0ea920551e5e5d626a85c262f.png
-->
    </div>

    <div class="post-copyright">
  <p class="copyright-item">
    <span class="item-title">文章作者</span>
    <span class="item-content">whowin</span>
  </p>
  <p class="copyright-item">
    <span class="item-title">上次更新</span>
    <span class="item-content">
        2022-12-10
        
    </span>
  </p>
  
  
</div>
<footer class="post-footer">
      <div class="post-tags">
          <a href="/tags/linux/">Linux</a>
          <a href="/tags/ubuntu/">ubuntu</a>
          <a href="/tags/netcat/">netcat</a>
          <a href="/tags/nc/">nc</a>
          <a href="/tags/udp-packet/">udp packet</a>
          </div>
      <nav class="post-nav">
        <a class="prev" href="/post/blog/network/0003-checksum-of-udp-header/">
            <i class="iconfont icon-left"></i>
            <span class="prev-text nav-default">如何计算UDP头的checksum</span>
            <span class="prev-text nav-mobile">上一篇</span>
          </a>
        <a class="next" href="/post/blog/network/0002-link-layer-programming/">
            <span class="next-text nav-default">Linux下如何在数据链路层接收原始数据包</span>
            <span class="next-text nav-mobile">下一篇</span>
            <i class="iconfont icon-right"></i>
          </a>
      </nav>
    </footer>
  </article>
        </div>
        

  <span id="/post/blog/network/0005-send-udp-via-linux-cli/" class="leancloud_visitors" data-flag-title="如何在Linux命令行下发送和接收UDP数据包">
		<span class="post-meta-item-text">文章阅读量 </span>
		<span class="leancloud-visitors-count">0</span>
		<p></p>
	  </span>
  <div id="vcomments"></div>
  <script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
  <script src='//unpkg.com/valine/dist/Valine.min.js'></script>
  <script type="text/javascript">
    new Valine({
        el: '#vcomments' ,
        appId: 'OFCGzCfJRUglzOdzrqMGkbTR-gzGzoHsz',
        appKey: 'v7P29kPAEbsmaavaYPNhGhnF',
        notify:  false ,
        verify:  false ,
        avatar:'mm',
        placeholder: '说点什么吧...',
        visitor:  true 
    });
  </script>

  

      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="social-links">
      <a href="mailto:hengch@163.com" class="iconfont icon-email" title="email"></a>
  <a href="https://whowin.gitee.io/index.xml" type="application/rss+xml" class="iconfont icon-rss" title="rss"></a>
</div>
<div class="copyright">
  <span class="power-by">
    由 <a class="hexo-link" href="https://gohugo.io">Hugo</a> 强力驱动
  </span>
  <span class="division">|</span>
  <span class="theme-info">
    主题 - 
    <a class="theme-link" href="https://github.com/olOwOlo/hugo-theme-even">Even</a>
  </span>

  <div class="busuanzi-footer">
    <span id="busuanzi_container_site_pv"> 本站总访问量 <span id="busuanzi_value_site_pv"><img src="/img/spinner.svg" alt="spinner.svg"/></span> 次 </span>
      <span class="division">|</span>
    <span id="busuanzi_container_site_uv"> 本站总访客数 <span id="busuanzi_value_site_uv"><img src="/img/spinner.svg" alt="spinner.svg"/></span> 人 </span>
  </div>

  <span class="copyright-year">
    &copy; 
    2022 - 
    2024<span class="heart"><i class="iconfont icon-heart"></i></span><span>whowin</span>
  </span>
</div>

    </footer>

    <div class="back-to-top" id="back-to-top">
      <i class="iconfont icon-up"></i>
    </div>
  </div>
  
  <script src="https://cdn.jsdelivr.net/npm/jquery@3.2.1/dist/jquery.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/slideout@1.0.1/dist/slideout.min.js" integrity="sha256-t+zJ/g8/KXIJMjSVQdnibt4dlaDxc9zXr/9oNPeWqdg=" crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.1.20/dist/jquery.fancybox.min.js" integrity="sha256-XVLffZaxoWfGUEbdzuLi7pwaUJv1cecsQJQqGLe7axY=" crossorigin="anonymous"></script>



<script type="text/javascript" src="/js/main.min.64437849d125a2d603b3e71d6de5225d641a32d17168a58106e0b61852079683.js"></script>
  <script type="text/javascript">
    window.MathJax = {
      tex: {
        inlineMath: [['$','$'], ['\\(','\\)']],
        }
    };
  </script>
  <script async src="https://cdn.jsdelivr.net/npm/mathjax@3.0.5/es5/tex-mml-chtml.js" integrity="sha256-HGLuEfFcsUJGhvB8cQ8nr0gai9EucOOaIxFw7qxmd+w=" crossorigin="anonymous"></script>








</body>
</html>
